package nl.vu.few.ibivu.biomodels.client.ui;

import java.util.HashMap;

import nl.vu.few.ibivu.biomodels.client.ui.cytoscapeweb.CytoscapeWeb;
import nl.vu.few.ibivu.biomodels.client.ui.view.ModelView;
import nl.vu.few.ibivu.biomodels.shared.PetrifierArcType;
import nl.vu.few.ibivu.biomodels.shared.PetrifierNodeType;

import com.smartgwt.client.types.SelectionType;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
import com.smartgwt.client.widgets.toolbar.ToolStripButton;

public class ModelViewToolbox extends Window {
	private ToolStrip toolStrip;
	private ToolStripButton newComponentButton;
	private ToolStripButton newInteractionButton;
	private ToolStripButton newActivationButton;
	private ToolStripButton newInhibitionButton;
	private ToolStripButton removeButton;
	private ToolStripButton clearButton;
	private CytoscapeWeb cytoscapeWeb;
	private ModelView.Presenter presenter;
	
	public ModelViewToolbox(CytoscapeWeb cytoscapeWeb) {
		super();
		this.cytoscapeWeb = cytoscapeWeb;
		
		setAutoSize(true);
		setStyleName("toolboxWindow");
		setTitle("Toolbox");
		setShowCloseButton(false);
		setShowMinimizeButton(false);
		setBackgroundColor("transparent");
		setBodyColor("transparent");
		HashMap<String,String> headerDefaults = new HashMap<String,String>();
		headerDefaults.put("height", "15");
		headerDefaults.put("width", "100");
		setAttribute("headerDefaults", headerDefaults, true);
		// Necessary to let autosize get rid of the minimum height
		setHeight(20);
		
		
		toolStrip = new ToolStrip();
		toolStrip.setStyleName("toolboxToolStrip");
		toolStrip.setLayoutTopMargin(5);
		toolStrip.setLayoutBottomMargin(5);
		toolStrip.setAutoWidth();

		//
		// New Component button initialization
		//
		toolStrip.addSpacer(5);
		newComponentButton = new ToolStripButton();
		newComponentButton.setIcon("[SKIN]ToolboxWindow/component.png");
		newComponentButton.setIconSize(36);
		newComponentButton.setActionType(SelectionType.RADIO);
		newComponentButton.setRadioGroup("nodeElementButtons");
		newComponentButton.setTooltip("New Gene");
		newComponentButton.setSelected(true);
		toolStrip.addButton(newComponentButton);
		toolStrip.addSpacer(5);

		//
		// New Interaction button initialization
		//
		newInteractionButton = new ToolStripButton();
		newInteractionButton.setIcon("[SKIN]ToolboxWindow/interaction.png");
		newInteractionButton.setIconSize(36);
		newInteractionButton.setActionType(SelectionType.RADIO);
		newInteractionButton.setRadioGroup("nodeElementButtons");
		newInteractionButton.setTooltip("New Interaction");
		toolStrip.addButton(newInteractionButton);

		toolStrip.addSpacer(5);
		toolStrip.addSeparator();
		toolStrip.addSpacer(5);

		//
		// New Activation button initialization
		//
		newActivationButton = new ToolStripButton();
		newActivationButton.setIcon("[SKIN]ToolboxWindow/activation.png");
		newActivationButton.setIconSize(36);
		newActivationButton.setActionType(SelectionType.RADIO);
		newActivationButton.setRadioGroup("arcElementButtons");
		newActivationButton.setTooltip("New Activation Arc");
		newActivationButton.setSelected(true);
		toolStrip.addButton(newActivationButton);
		toolStrip.addSpacer(5);

		//
		// New Inhibition button initialization
		//
		newInhibitionButton = new ToolStripButton();
		newInhibitionButton.setIcon("[SKIN]ToolboxWindow/inhibition.png");
		newInhibitionButton.setIconSize(36);
		newInhibitionButton.setActionType(SelectionType.RADIO);
		newInhibitionButton.setRadioGroup("arcElementButtons");
		newInhibitionButton.setTooltip("New Inhibition Arc");
		toolStrip.addButton(newInhibitionButton);

		toolStrip.addSpacer(5);
		toolStrip.addSeparator();
		toolStrip.addSpacer(5);

		//
		// New Remove elements button initialization
		//
		removeButton = new ToolStripButton();
		removeButton.setIcon("[SKIN]ToolboxWindow/rubber.png");
		removeButton.setShowRollOver(true);
		removeButton.setIconSize(36);
		removeButton.setTooltip("Remove Selected");
		removeButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ModelViewToolbox.this.presenter.invalidateModelData();;
				ModelViewToolbox.this.cytoscapeWeb.removeElements(
						ModelViewToolbox.this.cytoscapeWeb.selected(), true);
			}

		});
		toolStrip.addButton(removeButton);
		toolStrip.addSpacer(5);

		//
		// New Clear canvas button initialization
		//
		clearButton = new ToolStripButton();
		clearButton.setIcon("[SKIN]ToolboxWindow/trash.png");
		clearButton.setShowRollOver(true);
		clearButton.setIconSize(36);
		clearButton.setTooltip("Clear(!)");
		clearButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ModelViewToolbox.this.presenter.invalidateModelData();
				ModelViewToolbox.this.cytoscapeWeb.removeElements(null, true);
			}

		});
		toolStrip.addButton(clearButton);
		toolStrip.addSpacer(5);

		this.addItem(toolStrip);
	}
	
	public PetrifierNodeType getSelectedNodeElementType() {
		if (newComponentButton.isSelected()) {
			return PetrifierNodeType.COMPONENT;
		} else {
			return PetrifierNodeType.INTERACTION;
		}
	}
	
	public PetrifierArcType getSelectedArcElementType() {
		if (newActivationButton.isSelected()) {
			return PetrifierArcType.ACTIVATION;
		} else {
			return PetrifierArcType.INHIBITION;
		}
	}
	
	public void setPresenter(ModelView.Presenter presenter) {
		this.presenter = presenter;
	}
}
